package com.awesomego.earth;

import android.content.Context;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class TSRenderer implements GLSurfaceView.Renderer {
    private Context mContext;
    private Circle3DTexture mEarth;
    private Circle3DTexture mCloud;

    public TSRenderer(Context context) {
        mContext = context;
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        GLES20.glClearColor(1, 1, 1, 1);
        // mEarth = new Circle3DTexture(0.8f, R.mipmap.earth, R.mipmap.earthn);
        mEarth = new Circle3DTexture(0.8f, R.mipmap.earth, -1);
        mCloud = new Circle3DTexture(0.82f, R.mipmap.cloud, -1);
        GLES20.glEnable(GLES20.GL_DEPTH_TEST);
        GLES20.glEnable(GLES20.GL_BLEND);

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(80);
                    } catch (InterruptedException e) {
                    }

                    mEarth.angle[1] = mEarth.angle[1] + 1;
                    mCloud.angle[1] = mCloud.angle[1] + 1.2f;
                }
            }
        }).start();
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
        float ratio = (float) width / height;
        MatrixUtils.setCamera(0, 0, 30, 0, 0, 0, 0, 1, 0);
        MatrixUtils.setFrustum(-ratio, ratio, -1, 1, 20, 100);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
        GLES20.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);

        mEarth.drawSelf();
        mCloud.drawSelf();
    }

}